package org.dawning.shop.product.command.aggregate;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.commandhandling.model.AggregateMember;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.spring.stereotype.Aggregate;
import org.dawning.shop.common.commands.product.CreateProductCommand;
import org.dawning.shop.common.commands.product.OnSaleProductCommand;
import org.dawning.shop.common.commands.product.StopSellingProductCommand;
import org.dawning.shop.common.commands.product.UpdateProductCommand;
import org.dawning.shop.common.domain.product.Product;
import org.dawning.shop.common.domain.product.ProductItem;
import org.dawning.shop.common.domain.product.ProductId;
import org.dawning.shop.common.events.product.ProductCreateEvent;
import org.dawning.shop.common.events.product.ProductOnSaleEvent;
import org.dawning.shop.common.events.product.ProductStopSellingEvent;
import org.dawning.shop.common.events.product.ProductUpdateEvent;
import org.dawning.shop.product.common.ProductConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;


/**
 * Created by miou on 2017/5/23 0023.
 */
@Aggregate
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductAggregate {

    Logger logger = LoggerFactory.getLogger(ProductAggregate.class);
    //aggregate root
    @AggregateIdentifier
    private String id;

    private String status;

    @AggregateMember
    private Product product;



    @CommandHandler
    public ProductAggregate(CreateProductCommand command) throws Exception {
        logger.info("apply create product command : " + command.toString());
        apply(new ProductCreateEvent(command.getProductId(),command.getProduct()));

    }

    @CommandHandler
    public void on(UpdateProductCommand command){
        apply(new ProductUpdateEvent(command.getProductId(),command.getProduct()));
    }

    @CommandHandler
    public void on(OnSaleProductCommand command){
        logger.info("apply sale product command : "+command.toString());
        apply(new ProductOnSaleEvent(command.getProductId(),command.getProductStatus()));

    }


    @CommandHandler
    public void on(StopSellingProductCommand command){
        logger.info("apply sale product command : "+command.toString());
        apply(new ProductOnSaleEvent(command.getProductId(),command.getProductStatus()));

    }

    @EventHandler
    public void on(ProductCreateEvent event){
        this.id = event.getProductId();
        this.product = event.getProduct();
        this.status = ProductConstant.PRODUCT_STATUS_STOP_SELLING;
    }

    @EventHandler
    public void on(ProductUpdateEvent event){
        this.product = event.getProduct();
    }

    @EventHandler
    public void on(ProductOnSaleEvent event){
        this.status = event.getProductStatus();
    }

    @EventHandler
    public void on(ProductStopSellingEvent event){
        this.status = event.getProductStatus();
    }
}
